﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib;
using System.IO.Ports;

namespace 生命体征测试仪
{
    public class SysConfig
    {
#if (Web) 
        public static bool bWeb = true;
#endif
        public static SerialPort XPCom=null;
        public static SerialPort ErWenCom=null;
        public static SerialPort XinDianCom=null;
        public static SerialPort XueTangCom=null;
        public static SerialPort XueYaCom=null;
        public static SerialPort XueYangCom=null;
        public static SerialPort TimerCom=null;
        public static void OpenCOM()
        {
            if(bHaveShenGao)
            {
                XPCom = new SerialPort();
                XPCom.ReadBufferSize = 40960;
                XPCom.BaudRate = 9600;
                XPCom.PortName = SysConfig.XPComNum;
                XPCom.ReadTimeout = 500;
                XPCom.WriteTimeout = 500;
                if (!WFNetLib.WFGlobal.OpenSerialPort(ref XPCom,"控制器板"))
                {
                    XPCom = null;
                }
            }
            if(bHaveTiWen)
            {
                ErWenCom = new SerialPort();
                ErWenCom.ReadBufferSize = 40960;
                ErWenCom.BaudRate = 9600;
                ErWenCom.PortName = SysConfig.TiWenComNum;
                if (!WFNetLib.WFGlobal.OpenSerialPort(ref ErWenCom,"耳温"))
                {
                    ErWenCom = null;
                }
            }
            if(bHaveXinDian)
            {
                XinDianCom = new SerialPort();
                XinDianCom.ReadBufferSize = 40960;
                XinDianCom.BaudRate = 38400;
                XinDianCom.PortName = SysConfig.XinDianComNum;
                if (!WFNetLib.WFGlobal.OpenSerialPort(ref XinDianCom,"心电"))
                {
                    XinDianCom = null;
                }
            }
            if(bHaveXueTang)
            {
                XueTangCom = new SerialPort();
                XueTangCom.ReadBufferSize = 40960;
                XueTangCom.BaudRate = 38400;
                XueTangCom.PortName = SysConfig.XueTangComNum;
                XueTangCom.ReadTimeout = 500;
                XueTangCom.WriteTimeout = 500;
                if (!WFNetLib.WFGlobal.OpenSerialPort(ref XueTangCom,"血糖仪"))
                {
                    XueTangCom = null;
                }
            }
            if(bHaveXueYa)
            {
                XueYaCom = new SerialPort();
                XueYaCom.ReadBufferSize = 40960;
                XueYaCom.BaudRate = 9600;
                XueYaCom.PortName = SysConfig.XueYaComNum;
                if (!WFNetLib.WFGlobal.OpenSerialPort(ref XueYaCom,"血压计"))
                {
                    XueYaCom = null;
                }
            }
            if(bHaveXueYang)
            {
                XueYangCom = new SerialPort();
                XueYangCom.ReadBufferSize = 40960;
                XueYangCom.BaudRate = 38400;
                XueYangCom.PortName = SysConfig.XueYangComNum;
                if (!WFNetLib.WFGlobal.OpenSerialPort(ref XueYangCom,"血氧仪"))
                {
                    XueYangCom = null;
                }
            }
            if(bHaveTimer)
            {
                TimerCom = new SerialPort();
                TimerCom.ReadBufferSize = 40960;
                TimerCom.BaudRate = 9600;
                TimerCom.PortName = SysConfig.TimerComNum;
                TimerCom.ReadTimeout = 500;
                TimerCom.WriteTimeout = 500;
                if (!WFNetLib.WFGlobal.OpenSerialPort(ref TimerCom,"定时器"))
                {
                    TimerCom = null;
                }
            }
            if (bHaveXueZhi)
            {

            }
        }
        public static void CloseCOM()
        {
            try
            {
                XPCom.Close();
            }
            catch { }
            try
            {
                ErWenCom.Close();
            }
            catch { }
            try
            {
                XinDianCom.Close();
            }
            catch { }

            try
            {
                XueTangCom.Close();
            }
            catch { }
            try
            {
                XueYaCom.Close();
            }
            catch { }
            try
            {
                XueYangCom.Close();
            }
            catch { }
            try
            {
                TimerCom.Close();
            }
            catch { }

        }
        //SysConfig.ini中的信息
        public static string XPComNum;
        public static string TiWenComNum;
        public static string XueYaComNum;
        public static string XinDianComNum;
        public static string XueYangComNum;
        public static string XueTangComNum;
        public static string XueZhiComNum;
        public static string TimerComNum;

        public static bool bHaveShenGao;
        public static bool bHaveTiWen;
        public static bool bHaveXueYa;
        public static bool bHaveXinDian;
        public static bool bHaveXueYang;
        public static bool bHaveXueTang;
        public static bool bHaveXueZhi;
        public static bool bHaveTimer;
        public static bool bHaveZhiFangYi;
        public static ushort fjqd = 0;
        public static string SysConfigFilePath;
        
        public static void InitDeviceSet()
        {
            SysConfigFilePath = System.Windows.Forms.Application.StartupPath + "\\SysConfig.ini";
            XPComNum = iniFileOP.Read("DeviceSet Set", "ShenGaoCOM",SysConfigFilePath);
            TiWenComNum = iniFileOP.Read("DeviceSet Set", "TiWenCOM", SysConfigFilePath);
            XueYaComNum = iniFileOP.Read("DeviceSet Set", "XueYaCOM", SysConfigFilePath);
            XinDianComNum = iniFileOP.Read("DeviceSet Set", "XinDianCOM", SysConfigFilePath);
            XueYangComNum = iniFileOP.Read("DeviceSet Set", "XueYangCOM", SysConfigFilePath);
            XueTangComNum = iniFileOP.Read("DeviceSet Set", "XueTangCOM", SysConfigFilePath);
            XueZhiComNum = iniFileOP.Read("DeviceSet Set", "XueZhiCOM", SysConfigFilePath);
            TimerComNum = iniFileOP.Read("DeviceSet Set", "TimerCOM", SysConfigFilePath);
            
#if (Web)
            SoftDataProtect.InitDataFile(1);
            
#else
            SoftDataProtect.InitDataFile(3);
#endif
            try
            {
                fjqd = ushort.Parse(SoftDataProtect.GetSoftData(0));
            }
            catch// (System.Exception ex)
            {
                fjqd = 0xffff;
                SoftDataProtect.SetSoftData(fjqd.ToString(), 0);
            }
            if (BytesOP.GetBit(fjqd,(int)Common.FJQDIndex.bHaveShenGao))
                bHaveShenGao = true;
            else
                bHaveShenGao = false;
            if (BytesOP.GetBit(fjqd, (int)Common.FJQDIndex.bHaveTiWen))
                bHaveTiWen = true;
            else
                bHaveTiWen = false;
            if (BytesOP.GetBit(fjqd, (int)Common.FJQDIndex.bHaveXueYa))
                bHaveXueYa = true;
            else
                bHaveXueYa = false;
            if (BytesOP.GetBit(fjqd, (int)Common.FJQDIndex.bHaveXinDian))
                bHaveXinDian = true;
            else
                bHaveXinDian = false;
            if (BytesOP.GetBit(fjqd, (int)Common.FJQDIndex.bHaveXueYang))
                bHaveXueYang = true;
            else
                bHaveXueYang = false;
            if (BytesOP.GetBit(fjqd, (int)Common.FJQDIndex.bHaveXueTang))
                bHaveXueTang = true;
            else
                bHaveXueTang = false;
            if (BytesOP.GetBit(fjqd, (int)Common.FJQDIndex.bHaveXueZhi))
                bHaveXueZhi = true;
            else
                bHaveXueZhi = false;

            bHaveXueZhi = false;

            if (BytesOP.GetBit(fjqd, (int)Common.FJQDIndex.bHaveTimer))
                bHaveTimer = true;
            else
                bHaveTimer = false;
            if (BytesOP.GetBit(fjqd, (int)Common.FJQDIndex.bHaveZhiFangYi))
                bHaveZhiFangYi = true;
            else
                bHaveZhiFangYi = false;
        }
    }
}
